package jsyf.oa.service.business;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import jsyf.oa.dao.business.BusinessCopierDao;
import jsyf.oa.entity.business.BusinessCopierEntity;
import jsyf.oa.entity.business.BusinessEntity;
import jsyf.oa.facade.business.BusinessCopierFacade;

@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class BusinessCopierService implements BusinessCopierFacade {
    @Autowired
    private BusinessCopierDao businesscopierDao;

    @Override
    public String save(String jsonData) {
        BusinessEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), BusinessEntity.class);
        String mainId = entity.getMain().getId();
        businesscopierDao.removeByCondition(" AND FK_BUSINESS_ID = ? ", new String[]{mainId});
        //添加
        List<BusinessCopierEntity> businessCopierEntityList = entity.getCopiers();
        for (int i = 0; i < businessCopierEntityList.size(); i++) {
            //businessCopierEntityList.get(i).setFkBusinessId(mainId);
            this.businesscopierDao.add(businessCopierEntityList.get(i));
        }
        return entity.getMain().getId();
    }

    @Override
    public List<JSONObject> getByBusinessId(String moneyId) {
        return businesscopierDao.getJSONListByCondition(" AND FK_BUSINESS_ID = ?", new Object[]{moneyId});
    }
}
